#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	int n;
	string s[1002];
	cin>>n;
	for(int i=1;i<=n+1;i++){
		int posmao=0,posdian[4]={0};
		int countdian=0,countmao=0,a=0,b=0,c=0,d=0,e=0;
		getline(cin,s[i]);
		if(i!=1){
		for(int j=7;j<s[i].length();j++){
			if(s[i][j]=='.'){
			    countdian++;
			    posdian[countdian]=j;
		    }
			if(s[i][j]==':'){
			    countmao++;
			    posmao=j;
		    }
		}
		for(int j=7;j<posdian[1];j++){
			a=a+(s[i][j]-'0')*pow(10,posdian[1]-j-1);
		}
		for(int j=posdian[1]+1;j<posdian[2];j++){
			b=b+(s[i][j]-'0')*pow(10,posdian[2]-j-1);
		}
		for(int j=posdian[2]+1;j<posdian[3];j++){
			c=c+(s[i][j]-'0')*pow(10,posdian[3]-j-1);
		}
		for(int j=posdian[3]+1;j<posmao;j++){
			d=d+(s[i][j]-'0')*pow(10,posmao-j-1);
		}
		for(int j=posmao+1;j<s[i].length();j++){
			e=e+(s[i][j]-'0')*pow(10,s[i].length()-j-1);
		}
		if(a>255||b>255||c>255||d>255||e>65535||(s[i][7]=='0'&&a!=0)||(s[i][posdian[1]+1]=='0'&&b!=0)||(s[i][posdian[2]+1]=='0'&&c!=0)||(s[i][posdian[3]+1]=='0'&&d!=0)||(s[i][posmao+1]=='0'&&e!=0)||countdian!=3||countmao!=1){
			cout<<"ERR"<<endl;
		}
		else if(s[i][0]=='S'){
			int flag=0;
			for(int j=1;j<i;j++){
				if(s[i]==s[j]) flag=1;
			}
			if(flag==0) cout<<"OK"<<endl;
			else cout<<"FAIL"<<endl;
		}
		else if(s[i][0]=='C'){
			int nb=0;
			for(int j=2;j<i;j++){
				int flag=0;
				if(s[j].length()==s[i].length()){
				for(int k=8;k<s[j].length();k++){
					if(s[j][k]!=s[i][k]) flag=1;
				}
				if(flag==0&&s[j][0]=='S'){
					cout<<j-1<<endl;
					nb=2;
					break;
				}
			    }
			}
			if(nb!=2) cout<<"FAIL"<<endl;
		}
	    }
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
 } 
